package wiki.hitime.stl.daq.web.controller.api;

import wiki.hitime.stl.daq.bean.base.RespDTO;
import wiki.hitime.stl.daq.bean.dto.req.ExecuteTaskExecuteReqDTO;
import wiki.hitime.stl.daq.service.task.DataExecuteService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zhangdaoping
 * @create 2019-07-31 11:15
 */
@RestController
@RequestMapping("/api/task")
public class TaskController {

    private DataExecuteService dataExecuteService;

    public TaskController(DataExecuteService dataExecuteService) {
        this.dataExecuteService = dataExecuteService;
    }

    @PostMapping("/execute")
    public RespDTO executeTask(@Valid @RequestBody ExecuteTaskExecuteReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        dataExecuteService.executeTask(reqDTO);
        return RespDTO.success();
    }
}
